### Packages
library(ggplot2)
library(dplyr)
library(GGally)
### Figure 12.1 - data
blue_jays <- read.csv("./data/blue_jays.csv")
blue_jays$KnownSex <- factor(blue_jays$KnownSex, levels = c("F", "M"))
blue_jays %>% sapply(class)
BirdID KnownSex BillDepth BillWidth BillLength Head Mass Skull Sex
"character" "factor" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "integer"
blue_jays %>% head()
### Figure 12.1
ggplot(blue_jays, aes(Mass, Head)) +
geom_point(pch = 21, fill = "gray25", color = "white", size = 3) +
xlab("body mass (g)") +
ylab("head length (mm)") +
theme_minimal()
ggplot(blue_jays, aes(Mass, Head, fill = KnownSex)) +
geom_point(pch = 21, color = "white", size = 3) +
xlab("body mass (g)") +
ylab("head length (mm)") +
scale_fill_manual(
values = c(F = "#D55E00", M = "#0072B2"),
breaks = c("F", "M"),
labels = c("female birds ", "male birds"),
name = NULL,
guide = guide_legend(direction = "horizontal")
) +
theme_minimal() +
theme(
legend.position = "top",
legend.justification = "right"
)
### Figure 12.3
blue_jays$sex <- ifelse(blue_jays$KnownSex == "F", "female birds", "male birds")
blue_jays$sex <- factor(blue_jays$sex, levels = c("female birds", "male birds"))
ggplot(blue_jays, aes(Mass, Head, size = Skull, fill = KnownSex)) +
geom_point(pch = 21, color = "white") +
facet_wrap(~sex) +
xlab("body mass (g)") +
ylab("head length (mm)") +
scale_fill_manual(values = c(F = "#D55E00", M = "#0072B2"), guide = "none") +
scale_radius(
name = "skull size (mm)",
range = c(2, 7),
limits = c(28, 34),
breaks = c(28, 30, 32, 34),
guide = guide_legend(override.aes = list(fill = "gray40"))
) +
theme_minimal() +
theme(
strip.background = element_rect(
fill = "grey85", colour = "grey85",
linetype = 1, linewidth = 0.25
)
)
### Figure 12.4
ggpairs(blue_jays, columns=6:8, aes(color = KnownSex)) +
theme_light()
load("./data/forensic_glass.rda")
forensic_glass %>% select(-type, -RI, -Si) %>%
ggcorr(palette = "RdBu", label = TRUE)
load("./data/CO2_emissions.rda")
emissions_1970_2010 <- CO2_emissions %>% filter(year %in% c(1970, 2010)) %>%
tidyr::spread(year, emissions) %>% na.omit()
ggplot(emissions_1970_2010, aes(`1970`, `2010`)) +
geom_abline(slope = 1, color = "grey") +
geom_point(pch = 21, fill = "#0072B2D0", color = "white", size = 3) +
scale_x_log10(
limits = c(0.008, 125),
breaks = c(0.01, 0.1, 1, 10, 100),
labels = c("0.01", "0.1", "1.0", "10", "100"),
name = parse(text = "`1970 CO`[2]*` emissions (tons / person)`")
) +
scale_y_log10(
limits = c(0.008, 125),
breaks = c(0.01, 0.1, 1, 10, 100),
labels = c("0.01", "0.1", "1.0", "10", "100"),
name = parse(text = "`2010 CO`[2]*` emissions (tons / person)`")
) +
theme_classic()